﻿using QLTour.Biz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLTour.Gui
{
    public partial class FrmTour : Form
    {
        private List<TourBiz> listTour;

        public FrmTour()
        {
            InitializeComponent();
            listTour = new List<TourBiz>();
        }

        private void FrmTour_Load(object sender, EventArgs e)
        {
            listTour = TourBiz.GetAllTours();
            SetListToListView(listTour, lstDsTour);

            SetDiaDiemToCombo(DiaDiemBiz.GetAllDiaDiem(), cbDiaDiem);
        }
        public void LoadGiaTour(TourBiz tour,ListView listview)
        {
            listview.Items.Clear();
            
            foreach (var t in tour.GetAllGiaTour())
            {
                ListViewItem lvi = new ListViewItem(t.MaGia.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, t.Ten);
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem(lvi, t.GiaTri.ToString());
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem(lvi, t.BatDau.ToString("dd/MM/yyyy"));
                ListViewItem.ListViewSubItem sit3 = new ListViewItem.ListViewSubItem(lvi, t.KetThuc.ToString("dd/MM/yyyy"));
                lvi.SubItems.Add(sit);
                lvi.SubItems.Add(sit1);
                lvi.SubItems.Add(sit2);
                lvi.SubItems.Add(sit3);
                lvi.Tag = t;
                listview.Items.Add(lvi);
            }
        }

        private void SetDiaDiemToListBox(List<DiaDiemBiz> lstDD, ListBox cb)
        {
            cb.Items.Clear();
            foreach (var dd in lstDD)
            {
                cb.Items.Add(dd);
            }
        }

        private void SetDiaDiemToCombo(List<DiaDiemBiz> lstDD, ComboBox cb)
        {
            cb.Items.Clear();
            foreach (var dd in lstDD)
            {
                cb.Items.Add(dd);
            }
        }

        private void SetListToListView(List<TourBiz> lst, ListView lv)
        {
            lv.Items.Clear();

            foreach (var t in lst)
            {
                ListViewItem lvi = new ListViewItem(t.MaTour);
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, t.Ten);
                lvi.SubItems.Add(sit);
                lvi.Tag = t;
                lv.Items.Add(lvi);
            }
        }

        private void tabCtlTour_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnThemTour_Click(object sender, EventArgs e)
        {
            TourBiz tour = new TourBiz();
            tour.MaTour = txtMaTour.Text;
            tour.Ten = txtTen.Text;
            tour.TrongNuoc = (chkTrongNuoc.Checked == true);
            tour.Insert();

            int i = 1;
            foreach (DiaDiemBiz dd in lstDiaDiem.Items)
            {
                ThamQuanBiz tq = new ThamQuanBiz();
                tq.MaDiaDiem = dd.MaDiaDiem;
                tq.MaTour = tour.MaTour;
                tq.ThuTu = i;
                tq.Insert();
                i++;
            }

            listTour = TourBiz.GetAllTours();
            SetListToListView(listTour, lstDsTour);
        }

        private void btnThemDiaDiem_Click(object sender, EventArgs e)
        {
            if (cbDiaDiem.SelectedIndex < 0)
            {
                MessageBox.Show(this, "Chưa chọn địa điểm", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                foreach(DiaDiemBiz dd in lstDiaDiem.Items) 
                {
                    if (dd.MaDiaDiem == ((DiaDiemBiz)cbDiaDiem.Items[cbDiaDiem.SelectedIndex]).MaDiaDiem)
                    {
                        MessageBox.Show(this, "Đã có địa điểm này trong bảng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                lstDiaDiem.Items.Add(cbDiaDiem.Items[cbDiaDiem.SelectedIndex]);
            }
        }

        private void lstDsTour_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDsTour.SelectedItems.Count > 0)
            {
                TourBiz tour = (TourBiz)lstDsTour.SelectedItems[0].Tag;

                txtMaTour.Text = tour.MaTour;
                txtTen.Text = tour.Ten;
                chkTrongNuoc.Checked = tour.TrongNuoc;

                LoadGiaTour(tour, lstGiaTour);
                SetDiaDiemToListBox(tour.GetAllDiaDiem(), lstDiaDiem);
            }
        }

        private void btnLuuTour_Click(object sender, EventArgs e)
        {
            if (lstDsTour.SelectedItems.Count == 1)
            {
                TourBiz tour = (TourBiz)lstDsTour.SelectedItems[0].Tag;
                if (!txtMaTour.Text.Equals(tour.MaTour ))
                {
                    MessageBox.Show(this, "Không được chỉnh sửa mã tour!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                ThamQuanBiz.XoaHetDiaDiem(tour);
                tour.TrongNuoc = chkTrongNuoc.Checked;
                tour.Ten = txtTen.Text;
                tour.Update();

                int i = 1;
                foreach (DiaDiemBiz dd in lstDiaDiem.Items)
                {
                    ThamQuanBiz tq = new ThamQuanBiz();
                    
                    tq.MaDiaDiem = dd.MaDiaDiem;
                    tq.MaTour = tour.MaTour;
                    tq.ThuTu = i;
                    tq.Insert();

                    i++;
                }
            }
        }

        private void btnXoaDiaDiem_Click(object sender, EventArgs e)
        {
            if (lstDiaDiem.SelectedIndex < 0)
            {
                MessageBox.Show(this, "Chưa chọn địa điểm cần xóa!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                lstDiaDiem.Items.RemoveAt(lstDiaDiem.SelectedIndex);
            }
        }

        private void btnXoaTour_Click(object sender, EventArgs e)
        {
            if (lstDsTour.SelectedItems.Count == 1)
            {
                TourBiz tour = (TourBiz)lstDsTour.SelectedItems[0].Tag;

                if (MessageBox.Show(this, "Có muốn xóa tour: " + tour.Ten + " và dữ liệu liên quan?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    ThamQuanBiz.XoaHetDiaDiem(tour);
                    tour.XoaHetGia();
                    tour.Delete();

                    listTour = TourBiz.GetAllTours();
                    SetListToListView(listTour, lstDsTour);
                }
            }
        }

        private void lstGiaTour_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDsTour.SelectedItems.Count > 0)
            {
                GiaTourBiz giatour = (GiaTourBiz)lstGiaTour.SelectedItems[0].Tag;
                txtMaGia.Text = giatour.MaGia.ToString();
                txtTenGia.Text = giatour.Ten;
                txtGiaTri.Text = giatour.GiaTri.ToString();
                dtpBatDau.Value = (DateTime)giatour.BatDau;
                dtpKetThuc.Value = (DateTime)giatour.KetThuc;
                txtTenGia.Tag = "update";
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtTenGia.Tag = "add";
        }

        private void btnLuuGia_Click(object sender, EventArgs e)
        {
            
            string str = (string)txtTenGia.Tag;
            GiaTourBiz giatour = new GiaTourBiz();
            giatour.MaGia = int.Parse(txtMaGia.Text.ToString());
            giatour.Ten = txtTenGia.Text;
            giatour.GiaTri = int.Parse(txtGiaTri.Text.ToString());
            giatour.BatDau = dtpBatDau.Value;
            giatour.KetThuc = dtpKetThuc.Value;
            giatour.MaTour = txtMaTour.Text;
            if (str == null || str.Equals("add"))
            {
                giatour.Insert();
            }
            if (str.Equals("update"))
            {
                giatour.Update();
            }
        }
    }
}
